import os

import pandas as pd
from pandas import Series
from pandas.core.interchange.dataframe_protocol import DataFrame

from tools import _get_files_name as get_files_name
from tools import BASE_PATH_企业级应用课设
from tools import *
from common import utils


def get_学号_segment(files_name: Series) -> Series:
    pattern = PATTERN_报告
    extracted = utils.extract_parts_from_series(files_name, 2, pattern).astype(int)
    if utils.find_duplicates(extracted):
        exit(0)

    extracted.name = '学号'
    return extracted


def get_姓名_segment(files_name: Series) -> Series:
    pattern = PATTERN_报告
    extracted = utils.extract_parts_from_series(files_name, 3, pattern).astype(str)
    extracted.name = '姓名'
    return extracted


def get_real_日期_segment(files_name: Series) -> Series:
    pattern = PATTERN_报告
    extracted = utils.extract_parts_from_series(files_name, 4, pattern).astype(str)
    extracted.name = '提交日期'
    return extracted


def get_logical_日期_segment(files_name: Series) -> Series:
    pattern = PATTERN_报告
    extracted = utils.extract_parts_from_series(files_name, 5, pattern).astype(str)
    extracted.name = '逻辑日期'
    return extracted.astype(str)


def get_课设报告_提交记录(output_path=None) -> DataFrame:
    df_学生_信息 = get_学生_信息()

    se_files_name = get_files_name(f"{BASE_PATH_企业级应用课设}\提交\报告", return_type="name_only")
    se_files_name.name = '文件名'

    se_学号 = get_学号_segment(se_files_name)
    se_姓名 = get_姓名_segment(se_files_name)
    se_real_日期 = get_real_日期_segment(se_files_name)

    df_merged = pd.concat([se_学号, se_姓名, se_real_日期], axis=1)
    # combined = combine_series_to_dataframe(se_学号, se_姓名, se_real_日期)
    # combined['上机'] = 'Y'

    df_merged = merge_student_dataframes(df_学生_信息, df_merged)

    if output_path:
        df_merged.to_excel(output_path, index=False)
        print(f"\n✅ 课设报告提交记录统计完成! 文件已保存至: {output_path}\n")

    return df_merged


def calc_成绩_by_date(base_score: int, output_path=None) -> DataFrame:
    if base_score is None:
        raise ValueError("计算成绩需要有基础分 base_score")

    se_files_name = get_files_name(f"{BASE_PATH_企业级应用课设}/提交/报告")

    se_学号 = get_学号_segment(se_files_name)
    se_姓名 = get_姓名_segment(se_files_name)
    # se_real_日期 = get_real_日期_segment(se_files_name)
    se_logical_日期 = get_logical_日期_segment(se_files_name)

    se_logical_date = convert_mmdd_to_date(se_logical_日期)

    # 1. 找到最早日期
    min_date = se_logical_date.min()
    # 2. 计算每个日期与最早日期的天数差
    days_diff = (se_logical_date - min_date).dt.days

    se_成绩 = base_score - (days_diff + 1) // 2
    # print(se_成绩)
    df_merged = pd.DataFrame({'学号': se_学号, '姓名': se_姓名, '成绩': se_成绩})

    # 如果需要，保存到Excel
    if output_path:
        df_merged.to_excel(output_path, index=False)
        print(f"\n✅ 所有报告成绩合并完成! 文件已保存至: {output_path}")

    return df_merged


if __name__ == "__main__":
    df_学生_信息 = get_学生_信息()

    path1 = f"{BASE_PATH_企业级应用课设}\提交\报告提交_汇总.xlsx"
    df_提交记录 = get_课设报告_提交记录(path1)

    path2 = f"{BASE_PATH_企业级应用课设}\提交\报告提交_成绩.xlsx"

    df_成绩 = calc_成绩_by_date(28, path2)
    print(df_成绩)
